package com.rp.news.util.extent;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

@Slf4j
public class TestMain {

    public static void main(String[] args) {

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                5,
                20,
                10,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(10000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());


        long start = System.currentTimeMillis();

        for (int i = 0; i < 20; i++) {
            CompletableFuture.runAsync(() -> {
                System.out.println("===============这是业务逻辑==============");
            }, threadPoolExecutor);
        }
        System.out.println("执行20次所需时间 time = " + (System.currentTimeMillis() - start));


        System.out.println("--------------------------------------------------------------");

        start = System.currentTimeMillis();
        final CountDownLatch countDownLatch = new CountDownLatch(20);
        for (int i = 0; i < 20; i++) {
            CompletableFuture.runAsync(() -> {
                try {
                    System.out.println("===============这是业务逻辑==============");
                    Thread.sleep(20);
                } catch (Exception e) {
                    log.error("调用接口查询企业信息,异常:{}", e.getMessage());
                } finally {
                    countDownLatch.countDown();
                }
            }, threadPoolExecutor);
        }

        try {
            countDownLatch.await();
            System.out.println("(20次任务全部完成请求)执行20次所需时间 time = " + (System.currentTimeMillis() - start));

        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }


}
